﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace HDJ.Framework.Core
{
    public class TableTypeSupprotArray : TableTypeSupprotObjectBase
    {
       
        public override List<string> TypeCategory => new List<string>() { "array" };
        public override bool IsArray => true;
        public TableTypeSupprotArray(Dictionary<Type, TableTypeSupportBase> baseTypeSupports):base(baseTypeSupports)
        {

        }

        public override Type GetTableType(string typeFullName)
        {
            string newFullTypeName = null;

            string[] arrays = typeFullName.Split('[');
            string baseTypeStr = arrays[0];
            Type baseType = null;
            foreach (var item in baseTypeSupports.Values)
            {
                if (item.TypeStringSupport.Contains(baseTypeStr))
                {
                    baseType = item.TableType;
                    break;
                }
            }
            if (baseType != null)
            {
                newFullTypeName = typeFullName.Replace(baseTypeStr, baseType.FullName);
            }
            else
            {
                newFullTypeName = typeFullName;
            }
            
            return ReflectionUtils.GetTypeByTypeFullName(newFullTypeName, false);
        }


        protected override string Object2String(object value)
        {
            Type type = value.GetType();
            string result = "";
            if (type == typeof(Vector2[]))
            {
                Vector2[] v2Array = (Vector2[])value;
                for (int i = 0; i < v2Array.Length; i++)
                {
                    result += v2Array[i].ToString();
                    if (i < (v2Array.Length - 1))
                        result += ",";
                }
            }
            else if (type == typeof(Vector3[]))
            {
                Vector3[] v3Array = (Vector3[])value;
                for (int i = 0; i < v3Array.Length; i++)
                {
                    result += v3Array[i].ToString();
                    if (i < (v3Array.Length - 1))
                        result += ",";
                }
            }
            else
            {
                result = JsonUtils.ToJson(value);
            }
            return result;
        }


        protected override object String2Object(Type type, string data)
        {
            object value = null;
            if (type == typeof(Vector2[]))
            {
                string[] temp0 = TableUtils.StringSplitInStartAndEndChar(data, '(', ')');
                Vector2[] v2Array = new Vector2[temp0.Length];
                for (int x = 0; x < temp0.Length; x++)
                {
                    v2Array[x]= (Vector2)TableUtils.StringData2Vector(temp0[x]);
                }
                value = v2Array;
            }
            else if (type == typeof(Vector3[]))
            {
                string[] temp0 = TableUtils.StringSplitInStartAndEndChar(data, '(', ')');
                Vector3[] v3Array = new Vector3[temp0.Length];
                for (int x = 0; x < temp0.Length; x++)
                {
                    v3Array[x] = (Vector3)TableUtils.StringData2Vector(temp0[x]);
                }
                value = v3Array;
            }
            else
            {
                value = JsonUtils.FromJson(type, data);
            }
            return value;
        }
    }
}
